#ifndef ORG_WXW0303_COMMON_Uncaught_Exception_Counter_H_
#define ORG_WXW0303_COMMON_Uncaught_Exception_Counter_H_

#if defined(__GNUG__) || defined(__CLANG__)
#define COMMON_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS
namespace __cxxabiv1 {
struct __cxa_eh_globals;
extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept;
}
#elif defined(_MSC_VER) && (_MSC_VER >= 1400) // MSVC++ 8.0 or greater
#define COMMON_EXCEPTION_COUNT_USE_GETPTD
struct _tiddata;
extern "C" _tiddata* _getptd();
#else
#error "Unsupported platform, don't include this header."
#endif

/* 获取当前线程中 live exception object 的数目;
 * live exception object;也就是自抛出(throw,rethrow)之后,尚未进入到匹配的 catch 子句
 * 的对象.
 * @note 参考 unwind-cxx.h(src/lib/cppsup/unwind-cxx.h).
 * @note 参考 facebook/folly/detail/UncaughtExceptionCounter.h.
 */
inline unsigned int uncaughtExceptionsNum()
{
#if defined(COMMON_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS)
  return *(reinterpret_cast<unsigned int*>(static_cast<char*>(
      static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*)));
#elif defined(COMMON_EXCEPTION_COUNT_USE_GETPTD)
  // _getptd() returns a _tiddata* (defined in mtdll.h).
  // The offset below returns _tiddata::_ProcessingThrow.
  return *(reinterpret_cast<int*>(static_cast<char*>(
      static_cast<void*>(_getptd())) + sizeof(void*) * 28 + 0x4 * 8));
#endif
}

#endif // ORG_WXW0303_COMMON_Uncaught_Exception_Counter_H_
